// 生成一个 5Mb 的文件
const fs = require('fs')
const path = require('path')
const writename = path.resolve(__dirname, 'temp.txt')

const ws = fs.createWriteStream(writename)

function write(size){
    
    let i = 0
    function _write(){
        let flog = true;
        while(size > i && flog){
            var text = Math.floor(Math.random() * 10000) +''
            flog = ws.write(text); // false 写入通道已经满了
            i++
        }
        if(size <= i){
            ws.end()
            console.log('写入完成')
        }
    }
    ws.on('drain', ()=>{
        _write() // 写入通道清空 重新写入
    })
    _write()
}


write(1024 * 1024 )